﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataAdapter
{
    public partial class Form1 : Form
    {
        const string ConStr = "Data Source=.;Initial Catalog=ADO_NET_Simple;Integrated Security=True";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = ExecuteDataTable("select * from Students");
            dgvStudentList.DataSource = dt;
        }

        /// <summary>
        /// 获取 DataTable
        /// </summary>
        /// <param name="cmdText"></param>
        /// <returns></returns>
        private DataTable ExecuteDataTable(string cmdText)
        {
            //实例化一个 SqlDataAdapter
            SqlDataAdapter da = new SqlDataAdapter(cmdText, ConStr);
            //实例化一个 DataTable
            DataTable dt = new DataTable();
            //将数据填充到 dt
            da.Fill(dt);
            return dt;
        }
    }
}
